package com.qingcloud.adminbackend.service.impl;

import com.qingcloud.adminbackend.constant.WamingConstant;
import com.qingcloud.adminbackend.base.AjaxResult;
import com.qingcloud.adminbackend.base.Vuetable2PageData;
import com.qingcloud.adminbackend.entity.upgrade.EmbeddedBinDTO;
import com.qingcloud.adminbackend.entity.upgrade.EmbededRequest;
import com.qingcloud.adminbackend.mapper.ITdEmbeddedBinMapper;
import com.qingcloud.adminbackend.service.ITdEmbeddedBinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ITdEmbeddedBinServiceImpl implements ITdEmbeddedBinService {

    @Resource
    private ITdEmbeddedBinMapper embeddedBinMapper;

    /**
     * 分页查询
     *
     * @param embeddedBin
     * @return
     */
    public AjaxResult getEmbedbedBin(EmbededRequest embeddedBin) {
        List<EmbeddedBinDTO> embeddedBinList = embeddedBinMapper.search(embeddedBin, embeddedBin.getOffset(), embeddedBin.getPerPage());
        Integer count = embeddedBinMapper.count(embeddedBin);
        Map<String, Object> resultMap = new HashMap<>(2);
        resultMap.put("data", embeddedBinList);
        resultMap.put("pagination", new Vuetable2PageData(count.longValue(), embeddedBin.getPerPage(), embeddedBin.getPage()));
        return new AjaxResult(WamingConstant.SUCCESS, null, resultMap);
    }

    /**
     * 添加Bin文件
     *
     * @param embeddedBin
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult addEmbedbedBin(EmbeddedBinDTO embeddedBin) {
        Integer flag = embeddedBinMapper.save(embeddedBin);
        if (flag >= 1) {
            return new AjaxResult(WamingConstant.SUCCESS, null, null);
        } else {
            return new AjaxResult(WamingConstant.FAILURE, null, null);
        }
    }

    /**
     * 修改
     *
     * @param embeddedBin
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult updateEmbedbedBin(EmbeddedBinDTO embeddedBin) {
        Integer flag = embeddedBinMapper.update(embeddedBin);
        if (flag > 0) {
            return new AjaxResult(WamingConstant.SUCCESS, "修改成功", null);
        } else {
            return new AjaxResult(WamingConstant.FAILURE, "修改失败", null);
        }
    }

    /**
     * 删除失败
     *
     * @param ids
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult deleteEmbedbedBin(String[] ids) {
        List<String> idList = Arrays.asList(ids);
        Integer flag = embeddedBinMapper.deleteByIdList(idList);
        if (flag > 0) {
            return new AjaxResult(WamingConstant.SUCCESS, "删除成功", null);
        } else {
            return new AjaxResult(WamingConstant.FAILURE, "删除失败", null);
        }
    }

    /**
     * 根据ID查询
     *
     * @param id
     * @return
     */
    public EmbeddedBinDTO getEmbedbedBinById(String id) {
        return embeddedBinMapper.findEmbeddedBinById(id);
    }

    public AjaxResult findAllBin() {
        return new AjaxResult(WamingConstant.SUCCESS, "查询成功", embeddedBinMapper.findAllBin());
    }
}

